package com.ruoyi.room.controller;

import com.ruoyi.blindBoxManage.domain.BlindBoxProduct;
import com.ruoyi.blindBoxManage.service.IBlindBoxProductService;
import com.ruoyi.blindBoxManage.service.IProductTypeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.room.domain.RoomGood;
import com.ruoyi.room.service.IRoomGoodService;
import com.ruoyi.room.service.IRoomService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 奖池商品Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/room/good")
public class RoomGoodController extends BaseController {
    @Autowired
    private IRoomGoodService roomGoodService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IBlindBoxProductService blindBoxProductService;
    @Autowired
    private IProductTypeService productTypeService;
    /**
     * 查询奖池商品列表
     */
    @PreAuthorize("@ss.hasPermi('room:good:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomGood roomGood) {
        startPage();
        List<RoomGood> list = roomGoodService.selectRoomGoodList(roomGood);
        for (RoomGood good : list) {
            good.setRoomName(roomService.selectRoomById(good.getRoomId()).getName());
        }
        return getDataTable(list);
    }

    /**
     * 导出奖池商品列表
     */
    @PreAuthorize("@ss.hasPermi('room:good:export')")
    @Log(title = "奖池商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoomGood roomGood) {
        List<RoomGood> list = roomGoodService.selectRoomGoodList(roomGood);
        ExcelUtil<RoomGood> util = new ExcelUtil<RoomGood>(RoomGood.class);
        util.exportExcel(response, list, "奖池商品数据");
    }

    /**
     * 获取奖池商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:good:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(roomGoodService.selectRoomGoodById(id));
    }

    /**
     * 新增奖池商品
     */
    @PreAuthorize("@ss.hasPermi('room:good:add')")
    @Log(title = "奖池商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoomGood roomGood) {
        BlindBoxProduct blindBoxProduct = blindBoxProductService.selectBlindBoxProductById(roomGood.getGoodId());
        roomGood.setGoodLevelName(productTypeService.selectProductTypeById(blindBoxProduct.getProductTypeId()).getName());
        roomGood.setName(blindBoxProduct.getName());
        roomGood.setImg(blindBoxProduct.getPicture());
        return toAjax(roomGoodService.insertRoomGood(roomGood));
    }

    /**
     * 修改奖池商品
     */
    @PreAuthorize("@ss.hasPermi('room:good:edit')")
    @Log(title = "奖池商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoomGood roomGood) {
        return toAjax(roomGoodService.updateRoomGood(roomGood));
    }

    /**
     * 删除奖池商品
     */
    @PreAuthorize("@ss.hasPermi('room:good:remove')")
    @Log(title = "奖池商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomGoodService.deleteRoomGoodByIds(ids));
    }
}
